'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    // 设置session  ctx.session.isVisited
    if (!ctx.session.isVisited) {
      ctx.session.isVisited = 1;
    } else {
      ctx.session.isVisited++;
    }

    const { userInfo } = ctx.session
    const { userList: fansList, count: fansCount } = await this.service.userRelation.getUsersByFollowerId(userInfo.id);
    // 获取关注列表
    const { userList: followList, count: followCount } = await this.service.userRelation.getFollower(userInfo.id);
    // 获取关注人的博客列表
    const result = await this.service.blog.getFollowerBlogList({
      pageIndex: 0,
      pageSize: 10,
      userId: userInfo.id,
    });
    const { count, blogList } = result;

    // 获取 at 我的数量
    const atCount = await this.service.atRelation.getAtCount(userInfo.id)

    await ctx.render('index.html', {
      title: '盛邦微博',
      isNav: true,
      userData: {
        userInfo,
        fansData: {// 粉丝
          count: fansCount,
          fansList,
        },
        followData: {// 关注
          count: followCount,
          followList,
        },
        isMe: true,
        atCount,
      },
      blogList,
      count,
      pageIndex: 1,
      pageSize: 10,
    });
  }
  // 用户信息
  async settingPage() {
    const { userName } = this.ctx.params;
    console.log(this.ctx.url);
    this.ctx.body = {
      userName,
    };
  }

  // 加载更多
  async loadingMore() {
    const { userName, pageIndex } = this.ctx.params;
    this.ctx.body = {
      msg: '加载更多',
      userName,
      pageIndex,
    };
  }
  // 404页面
  async notFound() {
    await this.ctx.render('404.html', { isNav: true });
  }
}

module.exports = HomeController;
